home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / games / nhak_src.zip / AMIUNIX.C < prev    next >
C/C++ Source or Header  |  1993-03-16  |  4KB  |  164 lines

  1. /*    SCCS Id: @(#)amiunix.c    3.0    89/05/02
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. /* This file collects some Unix dependencies; pager.c contains some more */
  6.  
  7. /*
  8.  * The time is used for:
  9.  *    - seed for rand()
  10.  *    - year on tombstone and yymmdd in record file
  11.  *    - phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
  12.  *    - night and midnight (the undead are dangerous at midnight)
  13.  *    - determination of what files are "very old"
  14.  */
  15.  
  16. /* block some unused #defines to avoid overloading some cpp's */
  17. #define MONDATA_H
  18. #include "hack.h"   /* mainly for index() which depends on BSD */
  19.  
  20. #define     NOSTAT
  21.  
  22. #ifndef NOSTAT
  23. #include    <stat.h>
  24. static struct stat buf, hbuf;
  25. #endif
  26.  
  27. extern time_t time();
  28.  
  29. static struct tm *NDECL(getlt);
  30.  
  31. void
  32. setrandom()
  33. {
  34.     (void) Srand((unsigned int) time ((time_t *) 0));
  35. }
  36.  
  37. static struct tm *
  38. getlt()
  39. {
  40.     time_t date;
  41.     struct tm *localtime();
  42.  
  43.     (void) time((long *)(&date));
  44.     return(localtime(&date));
  45. }
  46.  
  47. int
  48. getyear()
  49. {
  50.     return(1900 + getlt()->tm_year);
  51. }
  52.  
  53. char *
  54. getdate()
  55. {
  56. #ifdef LINT    /* static char datestr[7]; */
  57.     char datestr[7];
  58. #else
  59.     static char datestr[7];
  60. #endif
  61.     register struct tm *lt = getlt();
  62.  
  63.     Sprintf(datestr, "%2d%2d%2d",
  64.         lt->tm_year, lt->tm_mon + 1, lt->tm_mday);
  65.     if(datestr[2] == ' ') datestr[2] = '0';
  66.     if(datestr[4] == ' ') datestr[4] = '0';
  67.     return(datestr);
  68. }
  69.  
  70. int
  71. phase_of_the_moon()                     /* 0-7, with 0: new, 4: full */
  72. {                    /* moon period: 29.5306 days */
  73.                     /* year: 365.2422 days */
  74.     register struct tm *lt = getlt();
  75.     register int epact, diy, golden;
  76.  
  77.     diy = lt->tm_yday;
  78.     golden = (lt->tm_year % 19) + 1;
  79.     epact = (11 * golden + 18) % 30;
  80.     if ((epact == 25 && golden > 11) || epact == 24)
  81.         epact++;
  82.  
  83.     return( (((((diy + epact) * 6) + 11) % 177) / 22) & 7 );
  84. }
  85.  
  86. int
  87. night()
  88. {
  89.     register int hour = getlt()->tm_hour;
  90.  
  91.     return(hour < 6 || hour > 21);
  92. }
  93.  
  94. int
  95. midnight()
  96. {
  97.     return(getlt()->tm_hour == 0);
  98. }
  99.  
  100. void
  101. gethdate(name)
  102. char *name;
  103. {
  104. #ifndef NOSTAT
  105. /* old version - for people short of space */
  106. /*
  107. /* register char *np;
  108. /*    if(stat(name, &hbuf))
  109. /*        error("Cannot get status of %s.",
  110. /*            (np = rindex(name, '/')) ? np+1 : name);
  111. /*
  112. /* version using PATH from: seismo!gregc@ucsf-cgl.ARPA (Greg Couch) */
  113.  
  114. /*
  115.  * The problem with   #include    <sys/param.h> is that this include file
  116.  * does not exist on all systems, and moreover, that it sometimes includes
  117.  * <sys/types.h> again, so that the compiler sees these typedefs twice.
  118.  */
  119. #define     MAXPATHLEN    80
  120.  
  121. extern char PATH[];    /* In amigaDOS.c */
  122.  
  123. register char *np, *path;
  124. char filename[MAXPATHLEN+1];
  125.  
  126.     if (index(name, '/') != NULL || (path = PATH) == NULL)
  127.     path = "";
  128.  
  129.     for (;;) {
  130.     if ((np = index(path, ':')) == NULL)
  131.         np = path + strlen(path);       /* point to end str */
  132.     if (np - path <= 1)                     /* %% */
  133.         (void) strcpy(filename, name);
  134.     else {
  135.         (void) strncpy(filename, path, np - path);
  136.         filename[np - path] = '/';
  137.         (void) strcpy(filename + (np - path) + 1, name);
  138.     }
  139.     if (stat(filename, &hbuf) == 0)
  140.         return;
  141.     if (*np == '\0')
  142.     path = "";
  143.     path = np + 1;
  144.     }
  145.     error("Cannot get status of %s.", (np = rindex(name, '/')) ? np+1 : name);
  146. #endif
  147. }
  148.  
  149. int
  150. uptodate(fd)
  151. {
  152.     return(1);
  153. }
  154.  
  155. void
  156. regularize(s)    /* normalize file name - we don't like :'s or /'s */
  157. register char *s;
  158. {
  159.     register char *lp;
  160.  
  161.     while((lp = index(s, ':')) || (lp = index(s, '/')))
  162.     *lp = '_';
  163. }
  164.